<template>
  <div class="oneway-export-dialog">
    <div class="export-options">
      <el-card shadow="never" class="option-card">
        <template #header>
          <div class="card-header">
            <el-icon><Download /></el-icon>
            <span>单程航班导出选项</span>
          </div>
        </template>

        <el-form label-width="120px">
          <!-- 筛选条件 -->
          <el-form-item label="航班类型">
            <el-radio-group v-model="exportOptions.flightType" class="export-radio-group">
              <el-radio value="all" class="export-radio-item">
                <div class="radio-option">
                  <div class="option-title">全部航班</div>
                  <div class="option-desc">（导出所有航班类型）</div>
                </div>
              </el-radio>
              <el-radio value="direct" class="export-radio-item">
                <div class="radio-option">
                  <div class="option-title">仅直飞航班</div>
                  <div class="option-desc">（只导出无中转的直飞航班）</div>
                </div>
              </el-radio>
              <el-radio value="transfer" class="export-radio-item">
                <div class="radio-option">
                  <div class="option-title">仅中转航班</div>
                  <div class="option-desc">（只导出包含中转的航班）</div>
                </div>
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="价格范围">
            <el-row :gutter="10">
              <el-col :span="11">
                <el-input-number
                    v-model="exportOptions.minPrice"
                    placeholder="最低价格"
                    :min="0"
                    style="width: 100%">
                </el-input-number>
              </el-col>
              <el-col :span="2" style="text-align: center; line-height: 32px;">
                <span>-</span>
              </el-col>
              <el-col :span="11">
                <el-input-number
                    v-model="exportOptions.maxPrice"
                    placeholder="最高价格"
                    :min="0"
                    style="width: 100%">
                </el-input-number>
              </el-col>
            </el-row>
          </el-form-item>

          <el-form-item label="航空公司">
            <el-select
                v-model="exportOptions.selectedAirlines"
                multiple
                placeholder="选择航空公司（不选则导出全部）"
                style="width: 100%">
              <el-option
                  v-for="airline in availableAirlines"
                  :key="airline.code"
                  :label="airline.name"
                  :value="airline.code">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="统计信息">
            <div class="export-stats">
              <el-tag type="info">当前筛选结果: {{ getFilteredFlights().length }} 个航班</el-tag>
              <el-tag type="success">
                预计导出: {{ getFilteredFlights().length }} 行数据
              </el-tag>
              <el-tag type="primary" v-if="getDirectCount() > 0">
                直飞: {{ getDirectCount() }} 个
              </el-tag>
              <el-tag type="warning" v-if="getTransferCount() > 0">
                中转: {{ getTransferCount() }} 个
              </el-tag>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>

    <div class="dialog-footer">
      <el-button @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="handleExport" :disabled="getFilteredFlights().length === 0">
        开始导出 ({{ getFilteredFlights().length }} 个航班)
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { reactive, computed } from 'vue'
import { Download } from '@element-plus/icons-vue'
import airlineNames from "@/utils/airlineNames.js"

// Props
const props = defineProps({
  flights: {
    type: Array,
    default: () => []
  },
  searchRequest: {
    type: Object,
    default: () => ({})
  }
})

// Emits
const emit = defineEmits(['export', 'close'])

// 响应式数据
const exportOptions = reactive({
  flightType: 'all',     // all | direct | transfer
  minPrice: null,
  maxPrice: null,
  selectedAirlines: []
})

// 计算属性
const availableAirlines = computed(() => {
  const airlineMap = new Map()

  props.flights.forEach(flight => {
    const code = flight.airlineCode
    if (code && !airlineMap.has(code)) {
      airlineMap.set(code, {
        code,
        name: airlineNames[code] || code
      })
    }
  })

  return Array.from(airlineMap.values()).sort((a, b) => a.name.localeCompare(b.name))
})

const getFilteredFlights = () => {
  let filtered = props.flights.slice()

  // 按航班类型筛选
  if (exportOptions.flightType === 'direct') {
    filtered = filtered.filter(flight => flight.transferCount === 0)
  } else if (exportOptions.flightType === 'transfer') {
    filtered = filtered.filter(flight => flight.transferCount > 0)
  }

  // 按价格范围筛选
  if (exportOptions.minPrice !== null) {
    filtered = filtered.filter(flight => flight.adultTotalPrice >= exportOptions.minPrice)
  }
  if (exportOptions.maxPrice !== null) {
    filtered = filtered.filter(flight => flight.adultTotalPrice <= exportOptions.maxPrice)
  }

  // 按航空公司筛选
  if (exportOptions.selectedAirlines.length > 0) {
    filtered = filtered.filter(flight =>
        exportOptions.selectedAirlines.includes(flight.airlineCode)
    )
  }

  return filtered
}

const getDirectCount = () => {
  return getFilteredFlights().filter(flight => flight.transferCount === 0).length
}

const getTransferCount = () => {
  return getFilteredFlights().filter(flight => flight.transferCount > 0).length
}

// 方法
const handleExport = () => {
  const filteredFlights = getFilteredFlights()
  emit('export', {
    selectedFlights: filteredFlights,
    exportOptions: { ...exportOptions },
    summary: {
      total: filteredFlights.length,
      direct: getDirectCount(),
      transfer: getTransferCount()
    }
  })
}
</script>

<style scoped>
.export-options {
  padding: 16px 0;
}

.option-card {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
}

.option-card .card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #409eff;
}

.export-radio-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.export-radio-item {
  margin-right: 0 !important;
  margin-bottom: 0 !important;
}

.radio-option {
  display: flex;
  flex-direction: column;
  margin-left: 4px;
}

.option-title {
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
}

.option-desc {
  font-size: 13px;
  color: #909399;
  line-height: 1.4;
}

.export-stats {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.export-stats .el-tag {
  align-self: flex-start;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}
</style>